//
//  GetPhoneNumber.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/29.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "GetPhoneNumber.h"
#import "JudgePhoneNumberBelong.h"

@implementation GetPhoneNumber

static GetPhoneNumber *_instance;
+ (instancetype)SharedGetPhoneNumber {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (void)getPhoneVerifyNumber:(UIButton *)verifyButton andPhoneNumber:(NSString *)phoneNumberString andView:(UIView *)addView {
    
    if ((phoneNumberString.length<11)||(![JudgePhoneNumberBelong isMobileNumber:phoneNumberString])) {
//        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号码输入有误,请重新输入" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alview show];
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"手机号码输入有误,请重新输入" toView:addView dimBackground:NO AndCompleteBlock:nil];
        
        [addView addSubview:hud];
        
    }else {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"验证码获取成功" toView:addView dimBackground:NO AndCompleteBlock:nil];
        
        [addView addSubview:hud];
        
        __block int timeout=59; //倒计时时间
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(_timer, ^{
            if(timeout<=0){ //倒计时结束，关闭
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    //设置界面的按钮显示 根据自己需求设置
                    [verifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                    verifyButton.backgroundColor = DEFAULT_COLOR;
                    verifyButton.userInteractionEnabled = YES;
                });
            }else{
                //            int minutes = timeout / 60;
                int seconds = timeout % 60;
                NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                dispatch_async(dispatch_get_main_queue(), ^{
                    //设置界面的按钮显示 根据自己需求设置
                    [verifyButton setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                    verifyButton.backgroundColor = [UIColor grayColor];
                    verifyButton.userInteractionEnabled = NO;
                    
                });
                timeout--;
                
            }
        });
        dispatch_resume(_timer);
        
        
        [self addPhoneMessage:phoneNumberString];
        NSLog(@"调方法");

    }
    
}

- (void)addPhoneMessage:(NSString *)phoneNumberString {
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"type":@"1",
                             @"phone":phoneNumberString
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"AppPub/Getcode" completeBlock:^(id object) {
        NSLog(@"%@",object);
    }];
    
}

- (void)getFotgetPhoneVerifyNumber:(UIButton *)verifyButton andPhoneNumber:(NSString *)phoneNumberString andView:(UIView *)addView {
    if ((phoneNumberString.length<11)||(![JudgePhoneNumberBelong isMobileNumber:phoneNumberString])) {
        //        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号码输入有误,请重新输入" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        //        [alview show];
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"手机号码输入有误,请重新输入" toView:addView dimBackground:NO AndCompleteBlock:nil];
        
        [addView addSubview:hud];
        
    }else {

        [self addPhoneForgetMessage:phoneNumberString andButton:verifyButton andView:addView];
    }
}

- (void)addPhoneForgetMessage:(NSString *)phoneNumberString andButton:(UIButton *)verifyButton andView:(UIView *)addView{
    
    NSDictionary *params = @{
                             @"username":phoneNumberString,
                             @"type":@"1",
                             @"phone":phoneNumberString
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"AppPub/Getcode" completeBlock:^(id object) {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:[object objectForKey:@"message"] toView:addView dimBackground:NO AndCompleteBlock:nil];
        
        [addView addSubview:hud];
        
        if (JudgeBackDataCode) {
            
            __block int timeout=59; //倒计时时间
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
            dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if(timeout<=0){ //倒计时结束，关闭
                    dispatch_source_cancel(_timer);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        [verifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                        verifyButton.backgroundColor = DEFAULT_COLOR;
                        verifyButton.userInteractionEnabled = YES;
                    });
                }else{
                    //            int minutes = timeout / 60;
                    int seconds = timeout % 60;
                    NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        [verifyButton setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                        verifyButton.backgroundColor = [UIColor grayColor];
                        verifyButton.userInteractionEnabled = NO;
                        
                    });
                    timeout--;
                    
                }
            });
            dispatch_resume(_timer);
            
            NSLog(@"调方法");
        }
    }];
    
}

@end
